import {  useLocation } from "react-router-dom";
import { useAccess } from "@/access";
import {useMemo} from 'react';
import { isNil } from 'lodash-es';
import {useCallback} from 'react';

export default () => {
  const { pathname } = useLocation();
  const { menuMap, menuMapId } = useAccess();
  const getIds = useCallback((id: any, menuMap: any) => {
    let list=[]
    if (!isNil(id)) {
      const item = menuMap?.[id]
      if (item) {
        list.push(item)
        if (!isNil(item.parentId)) {
          const children = getIds(item.parentId, menuMap)
          list=list.concat(children)

        }
      }
    }
    
    return list
  }, [])
  const currentList = useMemo(() => {
    const item = menuMap?.[pathname]
    const list = getIds(item?.id, menuMapId)
   return list?.reverse?.()
  }, [pathname, menuMap, menuMapId, getIds])
  
 
  return [currentList]
}